package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.AdminOperation;
import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.AdminPageDTO;
import com.etone.smartAudit.dto.BaseDTO;
import com.etone.smartAudit.dto.PageDTO;
import com.etone.smartAudit.dto.admin.AdminDTO;
import com.etone.smartAudit.dto.admin.PasswordDTO;
import com.etone.smartAudit.service.AdminMenuService;
import com.etone.smartAudit.service.AdminService;
import com.etone.smartAudit.vo.PUumUserinfoVO;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.admin.AdminDetailVO;
import com.etone.smartAudit.vo.admin.AdminListVO;
import com.etone.smartAudit.vo.admin.AdminSimpleVO;
import com.etone.smartAudit.vo.menu.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * @author: sovy
 * @date: 2020-06-03 14:44:50
 * @description:
 */
@AllArgsConstructor
@RestController
@Api(tags = "管理员", value = "AdminController")
public class AdminController {

    private AdminService adminService;

    private AdminMenuService adminMenuService;

    @ApiOperation(value = "管理员列表《完》")
    @GetMapping("/admins")
    public PagerVO<AdminListVO> list(
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize,
            @ApiParam(value = "关键字(用户名称，公司名称)") @RequestParam(required = false) String keys,
            @ApiParam(value = "用户名称") @RequestParam(required = false) String nickName,
            @ApiParam(value = "角色id") @RequestParam(required = false) Integer roleId,
            @ApiParam(value = "角色（R_ADMIN:管理员，R_AUDIT：内审管理员，R_OFFICE：事务所，R_IT：IT运维组）") @RequestParam(required = false) List<String> roleCodes,
            @ApiParam(value = "创建时间:开始时间") @RequestParam(required = false) Date startTime,
            @ApiParam(value = "创建时间:结束时间") @RequestParam(required = false) Date endTime
    ) {
        return adminService.list(keys, nickName, roleId, roleCodes, startTime, endTime, page, pageSize);
    }

    @ApiOperation(value = "管理员基本列表《完》")
    @GetMapping("/admins/simple")
    public PagerVO<AdminSimpleVO> listSimple(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize,
            @ApiParam(value = "关键字(用户名称，公司名称)") @RequestParam(required = false) String keys,
            @ApiParam(value = "权限(task_checking_member:审核人成员，task_exec_member:执行人成员)") @RequestParam(required = false) List<String> permissionTypes,
            @ApiParam(value = "角色id") @RequestParam(required = false) List<Integer> roleIds,
            @ApiParam(value = "专家id") @RequestParam(required = false) List<Integer> serviceExpertIds
    ) {
        return adminService.listSimple(admin, keys, permissionTypes, roleIds, serviceExpertIds, page, pageSize);
    }


    @ApiOperation(value = "获取系统下面的人员")
    @GetMapping("/admin/companyUsers")
    public PagerVO<PUumUserinfoVO> getCompanyUsers(AdminPageDTO pageDTO) {
        return adminService.getCompanyUsers(pageDTO);
    }

    @ApiOperation(value = "管理员详情《完》")
    @GetMapping("/admins/{id}")
    public AdminDetailVO detail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return adminService.getAdminVO(id);
    }

    @ApiOperation(value = "判断用户名是否存在，true：已存在，false：未存在《完》")
    @GetMapping("/admins/accountExists/{account}")
    public Boolean accountExists(
            @ApiParam(value = "account", required = true) @PathVariable String account
    ) {
        return adminService.accountExists(account);
    }

    @AdminOperation(module = "账号操作", item = "添加")
    @ApiOperation(value = "增加管理员《完》")
    @PostMapping("/admins")
    public void add(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "表单详情") @RequestBody @Validated AdminDTO adminDTO
    ) {
        adminService.save(adminDTO, null, admin);
    }

    @AdminOperation(module = "账号操作", item = "编辑")
    @ApiOperation(value = "编辑管理员《完》")
    @PutMapping("/admins/{id}")
    public void edit(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "表单详情") @RequestBody @Validated({BaseDTO.Edit.class}) AdminDTO adminDTO
    ) {
        adminService.save(adminDTO, id, null);
    }

    @ApiOperation(value = "删除管理员《完》")
    @DeleteMapping("/admins/{id}")
    @AdminOperation(module = "账号操作", item = "删除")
    public void delete(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiIgnore @LoginAdmin Admin loginAdmin
    ) {
        adminService.delete(id, loginAdmin);
    }

    @ApiOperation(value = "批量删除管理员《完》")
    @DeleteMapping("/admins/batch/{ids}")
    @AdminOperation(module = "账号操作", item = "批量删除")
    public void delete(
            @ApiParam(value = "ids") @PathVariable List<Integer> ids,
            @ApiIgnore @LoginAdmin Admin loginAdmin
    ) {
        adminService.delete(ids, loginAdmin);
    }

    @ApiOperation(value = "获取管理员菜单《完》")
    @GetMapping("/admins/menus")
    public List<MenuVO> findAdminMenus(
            @ApiIgnore @LoginAdmin Admin admin
    ) {
        return adminMenuService.findAdminMenus(admin);
    }

    @ApiOperation(value = "修改密码《完》")
    @PostMapping("/admins/password")
    public void updatePwd(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "密码表单") @RequestBody PasswordDTO passwordDTO
    ) {
        adminService.updatePwd(admin, passwordDTO);
    }

}